package com.zhc.dejingapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhc.dejingapi.common.result.Result;
import com.zhc.dejingapi.common.result.ResultCodeEnum;
import com.zhc.dejingapi.common.utils.SerialNumUtil;
import com.zhc.dejingapi.entity.QingInjectionmoldsendworkdetailEntity;
import com.zhc.dejingapi.entity.QingProcessplanEntity;
import com.zhc.dejingapi.entity.query.QingInQuery;
import com.zhc.dejingapi.entity.vo.IdAndCode;
import com.zhc.dejingapi.mapper.QingInjectionmoldsendworkdetailMapper;
import com.zhc.dejingapi.mapper.QingProcessplanMapper;
import com.zhc.dejingapi.service.QingInjectionmoldsendworkdetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhc
 * @since 2024-10-07
 */
@Service
public class QingInjectionmoldsendworkdetailServiceImpl extends ServiceImpl<QingInjectionmoldsendworkdetailMapper, QingInjectionmoldsendworkdetailEntity> implements QingInjectionmoldsendworkdetailService {

    @Resource
    private QingInjectionmoldsendworkdetailMapper qingInjectionmoldsendworkdetailMapper;

    @Resource
    private QingProcessplanMapper qingProcessplanMapper;

    @Resource
    private SerialNumUtil serialNumUtil;
    @Override
    public Result addSendWork(List<QingInjectionmoldsendworkdetailEntity> qingInjectionList) {
        int i=0;

        for (QingInjectionmoldsendworkdetailEntity qingIn : qingInjectionList){
            int insert = qingInjectionmoldsendworkdetailMapper.insert(qingIn);
            if(i>0){
                QueryWrapper<QingProcessplanEntity> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("process_plan_code",qingIn.getProcessPlanCode());
                QingProcessplanEntity qingProcessplanEntity = qingProcessplanMapper.selectOne(queryWrapper);
                Integer totalSendWorkNum = qingProcessplanEntity.getTotalSendWorkNum();
                if(totalSendWorkNum==null||totalSendWorkNum==0){
                    totalSendWorkNum=qingIn.getSendWorkNum();
                }else {
                    totalSendWorkNum=totalSendWorkNum+qingIn.getSendWorkNum();
                }
                qingProcessplanEntity.setTotalSendWorkNum(totalSendWorkNum);
                int update = qingProcessplanMapper.update(qingProcessplanEntity, queryWrapper);
//                if(update>0){
//
//                }
            }
            if(insert>0){
                i++;
            }

        }
        if(i==qingInjectionList.size()){
            return Result.build(null, ResultCodeEnum.SEND_WORK_ADD_SUCCESS);
        }else {
            return Result.build(null, ResultCodeEnum.SEND_WORK_ADD_FAIL);
        }

    }

    @Override
    public Result deleteSendWork(Long mainId) {

        QingInjectionmoldsendworkdetailEntity qingInjectionmoldsendworkdetailEntity = qingInjectionmoldsendworkdetailMapper.selectById(mainId);
        QueryWrapper<QingProcessplanEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("process_plan_code",qingInjectionmoldsendworkdetailEntity.getProcessPlanCode());
        QingProcessplanEntity qingProcessplanEntity = qingProcessplanMapper.selectOne(queryWrapper);
        int i = qingInjectionmoldsendworkdetailMapper.deleteById(mainId);
        if (i>0){
            Integer sendWorkNum = qingProcessplanEntity.getTotalSendWorkNum();
            sendWorkNum=sendWorkNum-qingInjectionmoldsendworkdetailEntity.getSendWorkNum();
            qingProcessplanEntity.setTotalSendWorkNum(sendWorkNum);
            qingProcessplanMapper.update(qingProcessplanEntity,queryWrapper);
            return Result.build(null,ResultCodeEnum.SEND_WORK_DELETE_SUCCESS);
        }else {
            return Result.build(null,ResultCodeEnum.SEND_WORK_DELETE_FAIL);
        }
    }

    @Override
    public Result updateIdAndCode(IdAndCode idAndCode) {
        QingInjectionmoldsendworkdetailEntity qingInjectionmoldsendworkdetailEntity = new QingInjectionmoldsendworkdetailEntity();
        qingInjectionmoldsendworkdetailEntity.setApplyId(idAndCode.getApplyId());
        qingInjectionmoldsendworkdetailEntity.setZsSendWorkCode(idAndCode.getZsSendWorkCode());
        QueryWrapper<QingInjectionmoldsendworkdetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("main_id",idAndCode.getMainId());
        int update = qingInjectionmoldsendworkdetailMapper.update(qingInjectionmoldsendworkdetailEntity, queryWrapper);
        if(update>0){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    @Override
    public Result selectQingIn(QingInQuery qingInQuery) {
        QueryWrapper<QingInjectionmoldsendworkdetailEntity> queryWrapper = new QueryWrapper<>();
        if(qingInQuery.getSendWorkDate()!=null){
            queryWrapper.eq("send_work_date",qingInQuery.getSendWorkDate());
        }
        if(qingInQuery.getWorkCenterName()!=null){
            queryWrapper.eq("work_center_name",qingInQuery.getWorkCenterName());
        }


        List<QingInjectionmoldsendworkdetailEntity> list = qingInjectionmoldsendworkdetailMapper.selectList(queryWrapper);
        return Result.ok(list);
    }

    @Override
    public List<QingInjectionmoldsendworkdetailEntity> getJobList(Long eId) {

        QueryWrapper<QingInjectionmoldsendworkdetailEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("eq_code",eId+"#").eq("send_work_sts","生产中");
        return qingInjectionmoldsendworkdetailMapper.selectList(queryWrapper);

    }

    @Override
    public QingInjectionmoldsendworkdetailEntity getInfoById(Long mainId) {
        return qingInjectionmoldsendworkdetailMapper.getInfoById(mainId);
    }

    @Override
    public Result updateSendWork(List<QingInjectionmoldsendworkdetailEntity> qingInjectionList) {
        int i=0;
        for (QingInjectionmoldsendworkdetailEntity qingInject:qingInjectionList) {
            QueryWrapper<QingInjectionmoldsendworkdetailEntity> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("main_id",qingInject.getMainId());
            int update = qingInjectionmoldsendworkdetailMapper.update(qingInject, queryWrapper);
            if (update>0){
                i++;
            }
        }

        if(i==qingInjectionList.size()){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }

    }
}
